package com.fa4j.system.file.service;

import com.fa4j.common.base.util.FillUtil;
import com.fa4j.system.file.SystemFileClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * 系统文件URL填充服务
 */
@Service
@RequiredArgsConstructor
public class FileUrlFillService {

    private final SystemFileClient systemFileClient;

    public <T> void fill(Collection<T> dataList, Function<T, Long> source, BiConsumer<T, String> target) {
        FillUtil.fillField(dataList, source, target, systemFileClient::queryFileUrlByIds);
    }

    public <T> void fill(T data, Function<T, Long> source, BiConsumer<T, String> target) {
        this.fill(List.of(data), source, target);
    }

    public String getFileUrl(Long fileId) {
        if (fileId == null) {
            return null;
        }
        Map<Long, String> fileUrlMap = systemFileClient.queryFileUrlByIds(List.of(fileId));
        return fileUrlMap.get(fileId);
    }

    public Map<Long/*文件ID*/, String/*文件URL*/> queryFileUrl(Collection<Long> fileIds) {
        if (fileIds == null || fileIds.isEmpty()) {
            return Map.of();
        }
        return systemFileClient.queryFileUrlByIds(fileIds);
    }
}
